package com.finger.forest;

import android.content.SharedPreferences;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.finger.forest.base.BaseApi;
import com.finger.forest.base.CommonKey;
import com.finger.forest.http.Api;
import com.finger.forest.utils.ClickUtils;
import com.finger.forest.utils.RequestUtil;
import com.finger.forest.utils.ToastUtils;
import com.finger.forest.utils.encode.PrefsUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectServiceActivity<saveSharedPreferences> extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "SelectServiceActivity";

    /**
     * 返回
     */
    @BindView(R.id.btn_back)
    ImageView backIv;

    /**
     * 保存服务器信息
     */
    @BindView(R.id.btn_save)
    Button saveBtn;

    /**
     * 重置
     */
    @BindView(R.id.btn_reset)
    Button resetBtn;

    /**
     * 服务器IP
     */
    @BindView(R.id.et_server_ip)
    EditText serverIpEt;

    /**
     * 服务器端口
     */
    @BindView(R.id.et_server_port)
    EditText serverPortEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_service);
        ButterKnife.bind(this);

        // 读取sharedpreference中的值
        String serverIp = PrefsUtil.getString(SelectServiceActivity.this, CommonKey.SERVER_IP);
        String serverPort = PrefsUtil.getString(SelectServiceActivity.this, CommonKey.SERVER_PORT);
        if (serverIp.isEmpty() || serverPort.isEmpty()){
            //没有添加服务器地址，就用默认的
            saveSharedPreferences();
            serverIp = PrefsUtil.getString(SelectServiceActivity.this, CommonKey.SERVER_IP);
            serverPort = PrefsUtil.getString(SelectServiceActivity.this, CommonKey.SERVER_PORT);
        }
        serverIpEt.setText(serverIp);
        serverPortEt.setText(serverPort);
    }

    /**
     * 存储到SharedPreferences
     */
    private void saveSharedPreferences(){
        String address = BaseApi.BASE_URL.substring(BaseApi.BASE_URL.indexOf("//")+2,BaseApi.BASE_URL.lastIndexOf("/"));
        String[] addressArr = address.split(":");
        PrefsUtil.setString(SelectServiceActivity.this, CommonKey.SERVER_IP, addressArr[0]);
        PrefsUtil.setString(SelectServiceActivity.this, CommonKey.SERVER_PORT, addressArr[1]);
    }

    @OnClick({R.id.btn_back,R.id.btn_save,R.id.btn_reset })
    @Override
    public void onClick(View v) {
        if (ClickUtils.isFastClick()) { //防止重复点击
            switch (v.getId()) {

                //返回
                case R.id.btn_back:
                    finish();
                    break;

                //保存服务器信息
                case R.id.btn_save:
                    PrefsUtil.setString(SelectServiceActivity.this, CommonKey.SERVER_IP, serverIpEt.getText().toString());
                    PrefsUtil.setString(SelectServiceActivity.this, CommonKey.SERVER_PORT, serverPortEt.getText().toString());
                    //设置请求地址、端口
                    Api.SERVER_IP = serverIpEt.getText().toString();
                    Api.SERVER_PORT = serverPortEt.getText().toString();
                    Log.i(TAG,"服务器SERVER_IP："+ Api.SERVER_IP);
                    ToastUtils.show("设置成功");
                    finish();
                    break;

                //重置
                case R.id.btn_reset:
                    serverIpEt.setText("");
                    serverPortEt.setText("");
                    break;

                default:
                    break;
            }
        }
    }

}
